﻿using System;
using BrnMall.Core;
using BrnMall.Sdk.QQMap;
using BrnMall.Services;
using WeiTap.Restful;
using BrnMall.Manager.Domain.System;
using BrnMall.Manager.Manager.System;

namespace QIDong.WeApp.Controllers
{
    public class CommonController : WebApi
    {
        /// <summary>
        /// 获取IP对应区域
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        /// <returns></returns>
        public RegionInfo GetRegionByGeo(float lat,float lng)
        {
            try
            {
                var geoLocation = QQMapApi.GetLocation(lat, lng);
                if(geoLocation?.result.address_component == null)
                    return new RegionInfo() { RegionId = -1, Name = "未知区域" };

                RegionInfo regionInfo = null;
                regionInfo = Regions.GetRegionByNameAndLayer(geoLocation.result.address_component.district, geoLocation.result.address_component.city, 3);
                return regionInfo ?? new RegionInfo() { RegionId = -1, Name = "未知区域" };
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            
        }
        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <param name="regionid"></param>
        /// <returns></returns>
        public RegionInfo GetRegionById(int regionid)
        {
            try
            {
                var region = Regions.GetRegionById(regionid);
                return region;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        #region 意见反馈
        /// <summary>
        /// 意见反馈
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="content">反馈内容</param>
        /// <param name="typeId">问题类型(1 客户服务, 2 功能异常, 3 产品建议)</param>
        /// <param name="mobile">反馈人的手机号</param>
        /// <param name="imgs">上传的图片，多张用逗号隔开</param>
        /// <returns></returns>
        public bool AddFeedback(int uid=0, string content="", int typeId=0, string mobile="", string imgs = "")
        {
            if (uid <=0)
                throw new Exception("用户id不能为空。");
            if (typeId == 1)
                throw new Exception("请选择类型");
            if (mobile == "")
                throw new Exception("请填写联系方式。");
            if (content == "")
                throw new Exception("反馈的内容不能为空");
            var model = new Feedback() { Uid = uid, Content = content, TypeId = typeId, Mobile = mobile, Imgs = imgs,AddTime=DateTime.Now };
            return FeedbackManager.Add(model);
        }
        #endregion
    }
}
